import java.util.ArrayList;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-17
 * Time: 12:11
 */
public class test1 {
    /*
     NewCoder BM46 最小的k个数
     */

    public ArrayList<Integer> GetLeastNumbers_Solution (int[] input, int k) {
        // write code here
        ArrayList<Integer> ret = new ArrayList<Integer>();
        if(input.length==0 || k==0){
            return ret;
        }
        PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2) -> o2.compareTo(o1));
        for(int i =0;i<k;i++){
            q.offer(input[i]);
        }
        for(int i=k;i<input.length;i++){
            if(q.peek()>input[i]){
                q.poll();
                q.offer(input[i]);
            }
        }
        for(int i =0;i<k;i++){
            ret.add(q.poll());
        }
        return ret;
    }
}
